package application.food;
import enums.CellType;
import application.Cell;
import application.Coordinate;
import application.GameMatrix;
/**
 * @author Adiel
 *
 */
public class FoodLocator {

	GameMatrix _matrix;
	private Coordinate _coordinate;
	private int _matrixSize;
	/**
	 * constructor
	 * @param matrix game matrix
	 */
	public FoodLocator(GameMatrix matrix) {
		this._matrix = matrix;
		this._matrixSize = this._matrix.get_dimension();
		this._coordinate = new Coordinate(this._matrixSize,this._matrixSize);
		this._coordinate = generateCoordinate();
	}
	/**
	 * chooses a random coordinate
	 * @return the coordinate
	 */
	private Coordinate generateCoordinate(){
		Coordinate tAns = null;
		int tXCoordinate = (int) (Math.random() * this._matrixSize);
		int tYCoordinate = (int) (Math.random() * this._matrixSize);
		while((tXCoordinate == _coordinate.get_x()) ||
				(tYCoordinate == _coordinate.get_y())){
			tXCoordinate = (int) (Math.random() * this._matrixSize);
			tYCoordinate = (int) (Math.random() * this._matrixSize);
		}
		tAns = new Coordinate(tXCoordinate,tYCoordinate);
		return tAns;
	}
	/**
	 * places a food on the board.
	 */
	public void locateFoodOnBoard(){
		Coordinate tCoordinate = generateCoordinate();
		int tXCoord = tCoordinate.get_x();
		int tYCoord = tCoordinate.get_y();
		CellType c = this._matrix.getCell(tXCoord, tYCoord).get_type();
		while(c != CellType.NONE){
			tCoordinate = generateCoordinate();
			tXCoord = tCoordinate.get_x();
			tYCoord = tCoordinate.get_y();
		}
		this._matrix.setCell(tXCoord, tYCoord, CellType.FOOD, randomFoodType());
		Cell c2 = this._matrix.getCell(this._coordinate);
		if(!CellType.isSnake(c2.get_type())){
			this._matrix.setCell(this._coordinate, CellType.NONE,null);
		}
		this._coordinate = tCoordinate;
	}
	/**
	 * 
	 * @return a random food type
	 */
	private Food randomFoodType() {
		Food f;
		int rand = (int) (Math.random() * 3);
		switch(rand){
			case 0 : 	f = new Meat(); 	break;
			case 1 : 	f = new IceCream(); break;
			case 2 : 	f = new Insect(); 	break;
			default : 	f = null; 			break;
		}
		return f;
	}

}
